﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geometry;

namespace PlanningGIS.MapSheet
{
    /// <summary>
    /// 图幅对象
    /// </summary>
    public class Mapsheet : IComparable
    {
        /// <summary>
        /// 图幅号
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 图幅名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 获取或设置图幅的选择状态
        /// </summary>
        public bool Checked { get; set; }

        /// <summary>
        /// 用户自定义数据
        /// </summary>
        public dynamic UserData { get; set; }

        /// <summary>
        /// 图幅空间范围
        /// </summary>
        public IGeometry Geometry { get; set; }

        /// <summary>
        /// 获取对象的Hash码
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return Code.GetHashCode();
        }

        /// <summary>
        /// 判断两个对象是否相等
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            if (obj is Mapsheet)
                return this.Code.Equals(((Mapsheet)obj).Code, StringComparison.CurrentCultureIgnoreCase);
            else
                return false;

        }

        /// <summary>
        /// 转成字符串
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return Code;
        }

        /// <summary>
        /// 比较两个图幅
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int CompareTo(object obj)
        {
            Mapsheet tmp = (Mapsheet)obj;
            IEnvelope thisExtent = this.Geometry.Envelope;
            IEnvelope cmpExtent = tmp.Geometry.Envelope;
            if (thisExtent.XMin == cmpExtent.XMin)
            {
                return thisExtent.YMin > cmpExtent.YMin ? 1 : -1;
            }
            else if (thisExtent.YMin == cmpExtent.YMin)
            {
                return thisExtent.XMin < cmpExtent.XMin ? 1 : -1;
            }
            else if (thisExtent.YMin > cmpExtent.YMin)
            {
                return 1;
            }
            return -1;
        }
    }

    /// <summary>
    /// 图幅比较
    /// </summary>
    public class MapsheetListEquality : IEqualityComparer<Mapsheet>
    {
        /// <summary>
        /// 比较两个图幅对象
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public bool Equals(Mapsheet x, Mapsheet y)
        {
            return x.Code.Equals(y.Code, StringComparison.CurrentCultureIgnoreCase);
        }

        /// <summary>
        /// 获取对象的Hash码
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int GetHashCode(Mapsheet obj)
        {
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return obj.ToString().GetHashCode();
            }
        }
    }


}
